/**
 * Copyright(c) 2014 Xunton Solutions
 *
 * History:
 *   15-4-16 上午10:35 Created by dwzhang
 */
package com.jade.framework.base.vfile.impl;

import java.util.Map;
import javax.inject.Named;

import com.jade.framework.base.vfile.VFileURIGetter;
import org.apache.commons.lang3.StringUtils;

/**
 * 简单的URI获取器，根据配置来组装VFILE的URI。
 *
 * @author <a href="mailto:dwzhang@xunton.com">dwzhang</a> 
 * @version 1.0 15-4-16 上午10:35
 */
@Named ("system_vfileURIGetter")
public class SimpleURIGetter
        implements VFileURIGetter
{
    private String defaultPath;
    private Map<String, String> pathMap;

    public void setDefaultPath(String defaultPath)
    {
        this.defaultPath = defaultPath;
    }

    public void setPathMap(Map<String, String> pathMap)
    {
        this.pathMap = pathMap;
    }

    public String getVFileURI(String type, String id)
    {
        if (StringUtils.isBlank(id)) {
            return id;
        }
        id = id.replace("\\", "/");

        String path = pathMap == null ? null : pathMap.get(type);
        path = StringUtils.isBlank(path) ? (defaultPath + "/" + type) : path;
        path = StringUtils.isBlank(path) ? "" : path;
        if (id.startsWith("/")) {
            return path + id;
        }
        else {
            return path + "/" + id;
        }
    }
}
